<?php
session_start();
include 'conn/conn.php';
if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$id=$_REQUEST["id"];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] ;
	
	//$targetPath="admin/".$id."/";
	$targetthumbnailPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder']."thumbs";
	$targetthumbnailPath1 = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder']."thumbs1";
	//$targetthumbnailPath ="admin/".$id."/thumbs";
	mkdir($targetPath);
	mkdir($targetthumbnailPath);
	mkdir($targetthumbnailPath1);
	//$targetFile =  $targetPath. time()."_".$_FILES['Filedata']['name'];
	
		$name = $_FILES['Filedata']['name']; 
		// Random 25 chars+nums . . . 
		$your_25_digit_code = substr(md5(rand(0,9999)), 17, 25); 
		// Get the file apart by ., so the 25 chars can be added before the extension 
		$temp_arr = explode(".", $name); 
		// Default it 
		$original_name = ''; 
		// Make sure the file isn't something like 
		// image.something.whatever.hi.hello.what.s.up.png 
		// Minus 2 because it would come out to 
		// (file_name).(random_chars).(extension), 
		// with this it comes out to (file_name)(random_chars).(extension) 
		for ($x = 0; $x < count($temp_arr)-2; $x++) { 
		   // Add to it 
		   $original_name .= $temp_arr[$x] . '.'; 
		} 
		// The new file name 
		$new_file = $original_name . $temp_arr[$x] . $your_25_digit_code . '.' . $temp_arr[$x+1]; 
		
		$filename=$new_file;
		$targetFile =  $targetPath. $filename;
	move_uploaded_file($tempFile,$targetFile);
	createThumbs($targetPath."/",$targetthumbnailPath."/",100);
		createThumbs($targetPath."/",$targetthumbnailPath1."/",400);
	$actimgpath =  $_REQUEST['folder']."".$filename;
	//$actimgpath =  "admin/".$id."/".$filename;
	$thumbimgpath =  $_REQUEST['folder']."thumbs/".$filename;
	//$thumbimgpath = "admin/".$id."/thumbs/".$filename;
	$thumbimgpath1 =  $_REQUEST['folder']."thumbs1/".$filename;
	$sql = 'INSERT INTO tbl_images (vehicleid,imagename,actimgpath,thumbimgpath,thumbimgpath1) 
	VALUES ('.$id.',"'.addslashes($filename).'","'.addslashes($actimgpath).'","'.addslashes($thumbimgpath).'","'.addslashes($thumbimgpath1).'")';	

	mysql_query($sql);
	echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
	
}
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth) 
{
  // open the directory
  $dir = opendir( $pathToImages );
 //$dir1 = opendir( $pathToImages1 );
  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' ) 
    {
      echo "Creating thumbnail for {$fname} <br />";

      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image 
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    }
  }
  // close the directory
  closedir( $dir );
 
}
?>